<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 登录头部logo部分 -->
      <div class="login_logo">
        <img src="@/assets/logo.png" alt>
      </div>
      <!-- 登录表单区域 -->
      <el-form
        label-width="0px"
        class="login_form"
        :model="formdata"
        :rules="rules"
        ref="loginForm"
      >
        <!-- 用户名 -->
        <el-form-item label prop="username">
          <el-input prefix-icon="iconfont icon-user" v-model="formdata.username"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="formdata.password"
            type="password"
          ></el-input>
        </el-form-item>

        <!-- 按钮部分 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="loginValid">登录</el-button>
          <el-button type="info" @click="loginReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.login_container {
  height: 100%;
  background: #2b4b6b;
}
.login_box {
  width: 450px;
  height: 300px;
  border-radius: 3px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_logo {
  width: 130px;
  height: 130px;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  top: 0px;
  transform: translate(-50%, -50%);
  box-shadow: 0px 0px 10px #ddd;
  padding: 10px;
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.login_form {
  width: 100%;
  position: absolute;
  bottom: 0;
  padding: 0 20px;
  box-sizing: border-box;
  .btn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
<script>
export default {
  data: function() {
    return {
      /* 登录表单的数据绑定对象 */
      formdata: {
        password: '123456',
        username: 'admin'
      },
      /* 登录表单数据验证规则 */
      rules: {
        /* 验证用户名是否合法 */
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          {
            min: 3,
            max: 5,
            message: '用户名长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        /* 验证密码是否合法 */
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '密码长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    /* 点击重置按钮，重置表单对象 */
    loginReset() {
      this.$refs.loginForm.resetFields()
      /* 表单实例方法调用 */
    },
    /* 登录按钮, 表单全体验证 */
    loginValid() {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return
        /* 在对象中取到data属性, 重命名为res */
        const { data: res } = await this.$http.post('login', this.formdata)
        console.log(res)

        if (res.meta.status === 200) {
          this.$message({
            message: '恭喜你，登录成功',
            type: 'success',
            duration: 1800
          })
          window.sessionStorage.setItem('token', res.data.token)
          setTimeout(() => {
            this.$router.push('/home')
          }, 1800)
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    }
  }
}
</script>
